﻿package com.jfdesgagne.jfdesgagne09 {
	import flash.display.Sprite;
	import flash.display.Stage;
	import com.jfdesgagne.jfdesgagne09.core.view.BackgroundView;
	import com.jfdesgagne.jfdesgagne09.core.controller.FrameController;
	import com.jfdesgagne.jfdesgagne09.core.service.RemotingGateway;
	import com.jfdesgagne.jfdesgagne09.core.model.ConfigurationModel;
	import com.jfdesgagne.jfdesgagne09.core.view.TooltipView;
	
	import com.jfdesgagne.jfdesgagne09.section.showcase.controller.ShowcaseController;
	
	import fl.lang.Locale;
	import flash.events.Event;
	import flash.display.StageAlign;
	import flash.display.StageDisplayState;
	import gs.plugins.*;
	import flash.external.ExternalInterface;
	import flash.display.LoaderInfo;
	
	public class JFDesgagne09 extends Sprite {
		private var _backgroundView:BackgroundView;
		public var tooltipView:TooltipView;
		private var _frameController:FrameController;
		public var gateway:RemotingGateway;
		public var configurationModel:ConfigurationModel;
		private var _resizeFunction_array:Array;
		private var _params:Object;
		private var _section:*;
		private var _oldSection:*;
		
		
		public function JFDesgagne09() {
			this._preLoad();
		}
		
		public function addToResize(_function:Function, _refClass:Object):void {
			this._resizeFunction_array.push({refFunction:_function, refClass:_refClass});
		}
		
		public function toggleFullscreen():void {
			if(this.stage.displayState != StageDisplayState.FULL_SCREEN) {
				this.stage.displayState = StageDisplayState.FULL_SCREEN;
			} else {
				this.stage.displayState = StageDisplayState.NORMAL;
			}
		}
		
		public function projectLoadedHandler(_data:Object):void {
			trace("loaded:"+_data);
		}
		
		public function showToolTip():void {
			//this._tooltipView.show(
		}
		
		public function changeSection($section:String):void {
			this.changeWindowLocation($section);
			if(this._section!=null) {
				this._oldSection = this._section;
				this._section.addEventListener(Event.UNLOAD, this._openSection);
				this._section.unload();
			}
			switch($section) {
				case "showcase":
					this._section = new ShowcaseController(this);
				break;
			}
			if(this._oldSection==null) this._openSection();
		}
		
		private function _openSection():void {
			this._section.show();
		}
		
		public function changeWindowLocation(path:String):void {
			ExternalInterface.call("changeWindowLocation", path);
		}
		
		private function _preLoad():void {
			this.configurationModel = new ConfigurationModel(LoaderInfo(this.root.loaderInfo).parameters);
			this.configurationModel.addEventListener(Event.COMPLETE, this._configurationLoadCompleteHandler);
		
			Locale.addXMLPath("en", this.configurationModel.http_address+"system/locale/en/jfdesgagne09_en.xml");
			Locale.addXMLPath("fr", this.configurationModel.http_address+"system/locale/fr/jfdesgagne09_fr.xml");
			Locale.setDefaultLang("en");
			Locale.setLoadCallback(this._localeLoadCompleteHandler);
			Locale.loadLanguageXML(Locale.getDefaultLang());
			}
		
		private function _isApplicationReady(event:Event=null):void {
			if(Locale.checkXMLStatus() && !this.configurationModel.hasEventListener(Event.COMPLETE)) {
				this._init();
			}
		}
		
		private function _configurationLoadCompleteHandler(event:Event):void {
			this.configurationModel.removeEventListener(Event.COMPLETE, this._configurationLoadCompleteHandler);
			this._isApplicationReady();
		}
		
		private function _localeLoadCompleteHandler(success:Boolean):void {
			if(success) this._isApplicationReady(); else trace('ERROR: loading locale file');
		}
		
		private function _init():void {
			this.gateway = new RemotingGateway(this.configurationModel.http_address+this.configurationModel.configuration_file.configuration.website.remoting_gateway);
			this.stage.align = StageAlign.TOP_LEFT;
			TweenPlugin.activate([RemoveTintPlugin, TintPlugin]);
			this._resizeFunction_array = new Array();
			this.stage.addEventListener(Event.RESIZE, this._resizeHandler);
			this._initView();
		}
		
		private function _resizeHandler(event:Event):void {
			for(var element in this._resizeFunction_array) {
				this._resizeFunction_array[element].refFunction.call(this._resizeFunction_array[element].refClass, event);
			}
		}
		
		private function _initView():void {
			this._backgroundView 	= new BackgroundView(this);
			this._frameController	= new FrameController(this);
			this.tooltipView		= new TooltipView(this);
			
		}
	}
	
}